package cn.com.oyat.idea.service;

import cn.com.oyat.idea.model.DlnaStatus;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AVTransportService {

    private DlnaStatus currentStatus = new DlnaStatus();

    public void setAVTransportURI(String videoUrl, String videoTitle, String clientIp) {
        currentStatus.setCurrentVideoUrl(videoUrl);
        currentStatus.setCurrentVideoTitle(videoTitle != null ? videoTitle : "未知视频");
        currentStatus.setStatus("准备播放: " + currentStatus.getCurrentVideoTitle());

        System.out.println("🎬 收到投屏请求:");
        System.out.println("   来源: " + clientIp);
        System.out.println("   标题: " + currentStatus.getCurrentVideoTitle());
        System.out.println("   URL: " + currentStatus.getCurrentVideoUrl());
        System.out.println("   时间: " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
    }

    public void play(String clientIp) {
        if (currentStatus.getCurrentVideoUrl() != null) {
            currentStatus.setStatus("正在播放: " + currentStatus.getCurrentVideoTitle());
            System.out.println("▶️ 播放命令来自: " + clientIp);
            System.out.println("   正在播放: " + currentStatus.getCurrentVideoTitle());
        } else {
            currentStatus.setStatus("错误: 没有可播放的视频");
            System.out.println("❌ 播放失败: 没有设置视频URL");
        }
    }

    public void stop(String clientIp) {
        currentStatus.setStatus("已停止");
        System.out.println("⏹️ 停止命令来自: " + clientIp);
    }

    public DlnaStatus getCurrentStatus() {
        return currentStatus;
    }

    public void clear() {
        currentStatus = new DlnaStatus();
    }
}